﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Fade.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.Modifier
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using starLiGHT.ParticleSystem.Common;

    #endregion

    /// <summary>
    /// The Fade modifier adjusts the particles alpha value as it ages and makes it transparent. It 
    /// uses the particles energy level to decide how transparent the color is to be displayed.
    /// <para>
    /// Usually a particle's energy changes from 1.0f to 0.0f over its lifetime, but
    /// this can be altered via the easing function set within the age modifier.<br/>
    /// </para>
    /// <para>
    /// This modifier should be used in conjunction with the Age modifier.
    /// </para>
    /// </summary>
    public class Fade : ModifierBase
    {
        #region Member Variables
        private float _diffAlpha;
        private float _endAlpha;

        #endregion

        #region Constructors
        /// <summary>
        /// The constructor creates a Fade modifier to be used by an emitter.
        /// </summary>
        /// <remarks>
        /// This modifier has a priority of -5, so that the alpha values is altered after 
        /// color changes.
        /// </remarks>
        public Fade()
            : this(1.0f, 0.0f)
        {
        }

        /// <summary>
        /// The constructor creates a Fade modifier to be used by an emitter.
        /// </summary>
        /// <remarks>
        /// This modifier has a priority of -5, so that the alpha values is altered after 
        /// color changes.
        /// </remarks>
        /// <param name="startAlpha">
        /// The alpha value for the particle when its energy level is 1.0f - usually at the start of its lifetime.
        /// This values have to be in the range of 0.0f and 1.0f. It is clamped when out of this range.
        /// </param>
        /// <param name="endAlpha">
        /// The alpha value for the particle when its energy level is 0.0f - usually at the end of its lifetime.
        /// This values have to be in the range of 0.0f and 1.0f. It is clamped when out of this range.
        /// </param>
        public Fade(float startAlpha, float endAlpha)
        {
            Priority = -1;
            this._diffAlpha = MathHelper.Clamp(startAlpha, 0.0f, 1.0f) - MathHelper.Clamp(endAlpha, 0.0f, 1.0f);
            this._endAlpha = endAlpha;
        }

        #endregion

        /// <summary>
        /// The alpha value for the particle when its energy level is 1.0f - usually at the start of its lifetime.
        /// This values have to be in the range of 0.0f and 1.0f. It is clamped when out of this range.
        /// </summary>
        public float StartAlpha
        {
            get { return _endAlpha + _diffAlpha; }
            set { this._diffAlpha = MathHelper.Clamp(value, 0.0f, 1.0f) - _endAlpha; }
        }

        /// <summary>
        /// The alpha value for the particle when its energy level is 0.0f - usually at the end of its lifetime.
        /// This values have to be in the range of 0.0f and 1.0f. It is clamped when out of this range.
        /// </summary>
        public float EndAlpha
        {
            get 
            { 
                return _endAlpha; 
            }

            set
            {
                float a = MathHelper.Clamp(value, 0.0f, 1.0f);
                this._diffAlpha = this._endAlpha + this._diffAlpha - a;
                this._endAlpha = a;
            }
        }

        /// <summary>
        /// Sets the alpha value of the particle based on the values defined 
        /// and the particle's energy level.
        /// <p>This method is called by the emitter and need not be called by the user</p>
        /// </summary>
        /// <param name="emitter">The Emitter that created the particle.</param>
        /// <param name="particle">The particle to be updated.</param>
        /// <param name="time">The duration of the frame - used for time based updates.</param>
        public override void Update(Emitter emitter, Particle particle, float time)
        {
            float alpha = _endAlpha + _diffAlpha * particle.energy;
            byte byteAlpha = (byte)(alpha * 255);

            if (byteAlpha > 0)
            {
                particle.color.R = (byte)(particle.color.R / particle.color.A * 255);
                particle.color.G = (byte)(particle.color.G / particle.color.A * 255);
                particle.color.B = (byte)(particle.color.B / particle.color.A * 255);

                particle.color.A = byteAlpha;

                particle.color.R = (byte)(particle.color.R * particle.color.A / 255);
                particle.color.G = (byte)(particle.color.G * particle.color.A / 255);
                particle.color.B = (byte)(particle.color.B * particle.color.A / 255);
            }
            else
            {
                particle.color.R = 0;
                particle.color.G = 0;
                particle.color.B = 0;
                particle.color.A = 0;
            }
        }
    }
}
